/*
 * @Author: SuperficialL
 * @Date: 2020-07-03 01:09:26
 * @LastEditTime: 2022-05-29 13:21:43
 * @Description: 文章数据
 */
import { getArticles, getArticle, likeArticle } from "../../api/articles";

const getDefaultListData = () => {
  return {
    data: [],
    pagination: {},
  };
};

export const state = () => {
  return {
    list: {
      fetching: false,
      data: getDefaultListData(),
    },
    hotList: {
      fetching: false,
      data: []
    },
    detail: {
      fetching: false,
      data: {},
    },
  };
};

export const mutations = {
  // 文章列表状态
  updateListFetching(state, action) {
    state.list.fetching = action;
  },

  // 文章列表
  updateListData(state, data) {
    console.log(data, "data");
    state.list.data = data;
  },

  // 更新文章列表
  updateExistingListData(state, res) {
    console.log(res, "res");
    state.list.data.data.push(...res.data);
    state.list.data.pagination = res.pagination;
  },

  // 更新热门文章列表
  updateHotListData(state, action) {
    state.hotList.data = action.data;
  },

  // 更新热门文章状态
  updateHotListFetching(state, action) {
    state.hotList.fetching = action;
  },

  // 文章详情状态
  updateDetailFetching(state, action) {
    state.detail.fetching = action;
  },
  
  // 文章详情
  updateDetailData(state, action) {
    state.detail.data = action;
  },

  // 更新文章阅读全文状态
  updateDetailRenderedState(state, action) {
    Vue.set(
      state.detail.data,
      'isRenderedFullContent',
      action == null ? true : action
    );
  },

  // 喜欢某篇文章
  updateLikesIncrement(state) {
    const article = state.detail.data;
    article && article.meta.likes++;
  },
};

export const actions = {
  // 获取文章列表
  fetchList({ commit }, params = {}) {
    const isRestart = !params.page || params.page === 1;
    const isLoadMore = params.page && params.page > 1;
    // 清空已有数据
    isRestart && commit('updateListData', getDefaultListData());
    commit('updateListFetching', true);
    return getArticles(params)
    .then((res) => {
      console.log(res,isLoadMore, "res");
        isLoadMore
          ? commit('updateExistingListData', res.result)
          : commit('updateListData', res.result);
      })
      .finally(() => {
        commit('updateListFetching', false);
      })
  },

  // 获取热门文章列表
  fetchHotList({ commit }) {
    commit("updateHotListFetching", true)
    return getArticles( { is_top: 1 })
      .then((res) => {
        commit('updateHotListData', res.result);
      })
      .finally(() => {
        commit("updateHotListFetching", false)
      });
  },

  // 获取文章详情
  getArticleDetail({ commit }, article = {}) {
    commit("updateDetailFetching", true)
    return getArticle(article.article_id)
      .then((res) => {
        commit('updateDetailData', res.result);
      })
      .finally(() => {
        commit("updateDetailFetching", false)
      });
  },

  // 喜欢文章
  fetchLikeArticle({ commit }, article_id) {
    return likeArticle({article_id}).then((res) => {
      commit('updateLikesIncrement');
      return Promise.resolve(res);
    });
  },
};

export default {
  namespaced: true,
  state,
  mutations,
  actions
};